﻿using System.Text;

using CodeBuilder.Core.Domain.Models;

namespace Tszy.CodeBuilder.Features.Builders;

internal class IRepositoryClassBuilder
{
    public static void InterfaceExecute(CodeTable table, string modelName, string basenamespace)
    {
        var fileName = $"I{modelName}Repository";
        var interfaceCode = OnInterfaceExecute(table, modelName, basenamespace);

        Build($"{fileName}.cs", interfaceCode);
    }

    public static void ClassExecute(CodeTable table, string modelName, string basenamespace)
    {
        var fileName = $"{modelName}Repository";
        var classCode = OnClassExecute(table, modelName, basenamespace);

        Build($"{fileName}.cs", classCode);
    }

    private static void Build(string fileName, string template)
    {
        var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates");

        if (!Directory.Exists(dir))
            Directory.CreateDirectory(dir);

        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates", fileName);

        File.WriteAllText(path, template);
    }

    private static string OnInterfaceExecute(CodeTable table, string modelName, string basenamespace)
    {
        var tableDescription = string.IsNullOrWhiteSpace(table.Description) ? "" : $"{table.Description}{(table.Description.EndsWith("。") ? "" : "。")}";

        var codeBuilder = new StringBuilder();

        codeBuilder.AppendLine($"//============================================================================================");
        codeBuilder.AppendLine($"/*");
        codeBuilder.AppendLine($" * @brief {table.Description.Replace("表", "").Replace("。", "")}仓储。");
        codeBuilder.AppendLine($" * ");
        codeBuilder.AppendLine($" *********************************************************************************************/");
        codeBuilder.AppendLine($"");
        codeBuilder.AppendLine($"using CodeBuilder.Domain;");
        codeBuilder.AppendLine($"");
        codeBuilder.AppendLine($"namespace CodeBuilder.Infrastructure.Repositories;");
        codeBuilder.AppendLine($"");
        codeBuilder.AppendLine($"/// <summary>");
        codeBuilder.AppendLine($"/// {table.Description.Replace("表", "").Replace("。", "")}仓储。");
        codeBuilder.AppendLine($"/// </summary>");
        codeBuilder.AppendLine($"public interface I{modelName}Repository : IRepository<{modelName}>");
        codeBuilder.AppendLine($"{{");
        codeBuilder.AppendLine($"}}");

        return codeBuilder.ToString();
    }

    private static string OnClassExecute(CodeTable table, string modelName, string basenamespace)
    {
        var tableDescription = string.IsNullOrWhiteSpace(table.Description) ? "" : $"{table.Description}{(table.Description.EndsWith("。") ? "" : "。")}";

        var codeBuilder = new StringBuilder();

        codeBuilder.AppendLine($"//============================================================================================");
        codeBuilder.AppendLine($"/*");
        codeBuilder.AppendLine($" * @brief {table.Description.Replace("表", "").Replace("。", "")}仓储。");
        codeBuilder.AppendLine($" * ");
        codeBuilder.AppendLine($" *********************************************************************************************/");
        codeBuilder.AppendLine($"");
        codeBuilder.AppendLine($"using CodeBuilder.Domain;");
        codeBuilder.AppendLine($"using Tszy.Unlimited.Data.FreeSQL;");
        codeBuilder.AppendLine($"");
        codeBuilder.AppendLine($"namespace CodeBuilder.Infrastructure.Repositories;");
        codeBuilder.AppendLine($"");
        codeBuilder.AppendLine($"/// <summary>");
        codeBuilder.AppendLine($"/// {table.Description.Replace("表", "").Replace("。", "")}仓储。");
        codeBuilder.AppendLine($"/// </summary>");
        codeBuilder.AppendLine($"public class {modelName}Repository : RepositoryBase<{modelName}>, I{modelName}Repository");
        codeBuilder.AppendLine($"{{");
        codeBuilder.AppendLine($"    public {modelName}Repository(IFreeSQLRepository<{modelName}> repository) : base(repository)");
        codeBuilder.AppendLine($"    {{");
        codeBuilder.AppendLine($"    }}");
        codeBuilder.AppendLine($"}}");

        return codeBuilder.ToString();
    }

    public class Result
    {
        public string FileName { get; set; } = string.Empty;
        public string Template { get; set; } = string.Empty;
    }
}